package com.jiyun.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jiyun.pojo.*;
import com.jiyun.service.PersonalService;
import com.jiyun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/personal")
public class PersonalController {

    @Autowired
    private PersonalService personalService;

    @PostMapping("/findByTel")
    public DcDriver findDriverByTel(String tel) {
        return personalService.selectByTel(tel);
    }

    @GetMapping("/bankCard")
    public List<NCard> findCardList() {
        return personalService.findCardList();
    }

    @PostMapping("/updateAssetsQuan")
    public ResultVo updateAssetQuan(String tel) {
        int i = personalService.updateAssetsQuan(tel);
        return ResultVo.ok(i);
    }


    @PostMapping("/updateCurrentWithdrawal")
    public ResultVo updateCurrentWithdrawal(String tel, BigDecimal inputMoney) {
        int i = personalService.updateCurrentWithdrawal(tel, inputMoney);
        return ResultVo.ok(i);
    }

    @PostMapping("/insertWithdraw")
    public String insertNewWallet(String tel, Integer inputMoney) {
        try {
            personalService.insertNewWallet(tel, inputMoney);
            return "数据插入成功";
        } catch (Exception e) {
            return "数据插入失败";
        }
    }

    @GetMapping("/findAllWithdraw")
    public List<DcWithdraw> findAllWithdraw() {
        return personalService.findAllWithdraw();
    }


    @GetMapping("list")
    public List<RiderQueue> list(Integer did) {
        return personalService.findAll(did);
    }


    @GetMapping("findBar")
    public Result findBar(Integer did) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String format = sdf.format(RiderQueue.getQueueTime());
        List<RiderQueue> list = personalService.findAll(did);
//        Integer queueTime = personalService.findQueueTime();
        // 使用Stream流进行过滤和分组统计
        Map<String, Long> collect = list.stream()
                .filter(x -> !ObjectUtil.isNull(x.getQueueTime()))
                .collect(Collectors.groupingBy(f -> sdf.format(f.getQueueTime()), Collectors.counting()));

        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(collect)) {
            for (Object key : collect.keySet()) {
                xAxis.add(key.toString());
                yAxis.add(collect.get(key));
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("xAxis", xAxis);
        map.put("yAxis", yAxis);
        return Result.success(map);
    }

    @RequestMapping("zichanzonge")
    public BigDecimal zichanzonge(Integer did) {
        return personalService.zichanzonge(did);
    }

    @RequestMapping("dangyueshouru")
    public BigDecimal dangyueshouru(Integer did) {
        return personalService.dangyueshouru(did);
    }

    @RequestMapping("updateSumMoney")
    public void updateSumMoney(BigDecimal withdrawMoney, String tel, Integer driverId) {
        personalService.updateSumMoney(withdrawMoney, tel, driverId);
    }

    @RequestMapping("dangyuetixian")
    public BigDecimal dangyuetixian(Integer driverId) {
        return personalService.dangyuetixian(driverId);
    }

    @RequestMapping("driverPrice")
    public void driverPrice(Integer id, BigDecimal totalAssets) {
        personalService.driverPrice(id, totalAssets);
    }



    @RequestMapping("saveDriverPrice")
    public void saveDriverPrice(Integer id, BigDecimal totalAssets){
        personalService.saveDriverPrice(id, totalAssets);
    }


}
